﻿using System.Collections;
using System.Collections.Generic;
using System;
using System.Data.SqlClient;

namespace PointTracker 
{
    public enum UserRole
    {
        Admin = 0,
        Professor = 1,
        Scribe = 2,
        Guest = 3
    }

    public class User
    {
        private UserRole _role;
        private string _fname;
        private string _lname;
        private string _email;

        public User()
        {
            _role = UserRole.Guest;
            _fname = "";
            _lname = "";
            _email = "";
        }

        public User(UserRole role, string fname, string lname, string email)
        {
            _role = role;
            _fname = fname;
            _lname = lname;
            _email = email;
        }

        public UserRole role
        {
            get { return _role; }
            set { _role = value; }
        }

        public string fname
        {
            get { return _fname; }
            set { _fname = value; }
        }

        public string lname
        {
            get { return _lname; }
            set { _lname = value; }
        }

        public string email
        {
            get { return _email; }
            set { _email = value; }
        }

        public static User[] getUsers(int crn, UserRole aRole)
        {
             int arole = 3;

            if (aRole == UserRole.Admin)
            {
                arole = 0;
            }
            else if (aRole == UserRole.Professor)
            {
                arole = 1;
            }
            else if (aRole == UserRole.Scribe)
            {
                arole = 2;
            }
            else
            {
                arole =3;
            }

            List<List<String>> users = DatabaseManager.getUsers(crn, arole);

            PointTracker.User[] u = new PointTracker.User[users.Count];
            for (int i = 0; i < users.Count; i++)
            {
                if (!users[0].Equals(""))
                {
                    u[i].fname = users[i][0];
                    u[i].lname = users[i][1];
                    u[i].email = users[i][2];
                    int userRole = System.Int32.Parse(users[i][3]);
                    if (userRole == 0)
                    {
                        u[i].role = UserRole.Admin;
                    }
                    else if (userRole == 1)
                    {
                        u[i].role = UserRole.Professor;
                    }
                    else if (userRole == 2)
                    {
                        u[i].role = UserRole.Scribe;
                    }
                    else
                    {
                        u[i].role = UserRole.Guest;
                    }
                }
            }

            return u;
        }

        public static Boolean adduser(string first, string last, string e_mail, string pw, UserRole aRole, int crn)
        {
            Boolean retVal = false;
            int arole = 3;

            if (aRole == UserRole.Admin)
            {
                arole = 0;
            }
            else if (aRole == UserRole.Professor)
            {
                arole = 1;
            }
            else if (aRole == UserRole.Scribe)
            {
                arole = 2;
            }
            else
            {
                arole =3;
            }

            if (!first.Equals("") && !last.Equals("") && !e_mail.Equals("") && !pw.Equals("") && arole >= 0 && arole <= 3)
            {
               retVal = DatabaseManager.addUser(first, last, e_mail, pw.GetHashCode(), arole, crn);
            }
            return retVal;
        }
        

        public static User getUserInstance(string login, string password)
        {
            PointTracker.User user = new User() ;
            if (!login.Equals(""))
            {
                string _login = login;
                int temppw = password.GetHashCode();
                string[] userVals = DatabaseManager.getUser(_login, temppw);

                if (!userVals[0].Equals(""))
                {
                    user.fname = userVals[0];
                    user.lname = userVals[1];
                    user.email = userVals[2];
                    int userRole = System.Int32.Parse(userVals[3]);
                    if (userRole == 0)
                    {
                        user.role = UserRole.Admin;
                    }
                    else if (userRole == 1)
                    {
                        user.role = UserRole.Professor;
                    }
                    else if (userRole == 2)
                    {
                        user.role = UserRole.Scribe;
                    }
                    else
                    {
                        user.role = UserRole.Guest;
                    }
                }
                else
                {
                    return null;
                }
            }
            return user;
        }

    }
}
